package com.le.tester.leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述：有效的数字
 */
public class ValidNumber {

    public int mark(char c) {
        String s = String.valueOf(c);
        String s1 = s.toLowerCase();
        switch (s1) {
            case " ":
                return 0;
            case "+":
            case "-":
                return 1;
            case ".":
                return 3;
            case "e":
                return 4;
            default:
                if (c >= 48 && c <= 57) return 2;
        }
        return -1;
    }

    public boolean isNumber(String s) {
        int state = 0;
        int finals = 0b101101000;
        int[][] transfer = new int[][]{{0, 1, 6, 2, -1},
                {-1, -1, 6, 2, -1},
                {-1, -1, 3, -1, -1},
                {8, -1, 3, -1, 4},
                {-1, 7, 5, -1, -1},
                {8, -1, 5, -1, -1},
                {8, -1, 6, 3, 4},
                {-1, -1, 5, -1, -1},
                {8, -1, -1, -1, -1}};
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            int mark = mark(aChar);
            if (mark < 0) {
                return false;
            }
            state = transfer[state][mark];
            if (state < 0) {
                return false;
            }
        }
        return (finals & (1 << state)) > 0;

    }

    public static void main(String[] args) {
        ValidNumber validNumber = new ValidNumber();
        System.out.println(validNumber.isNumber("1E9"));
    }
}
